package org.example.old.converter;

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.util.StringUtils;
import com.alibaba.excel.write.merge.AbstractMergeStrategy;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;

public class DemoStyleStrategy extends AbstractMergeStrategy {
    @Override
    protected void merge(Sheet sheet, Cell cell, Head head, Integer integer) {
        // 获取单元格数据，并且转化为字符串，startValue用于判断改行是否合并。避免报错数据获取异常
        String startValue = null;
        if(cell.getCellType() == CellType.STRING){
            startValue = cell.getStringCellValue();
        }else if(cell.getCellType() == CellType.NUMERIC){
            startValue =  String.valueOf(cell.getNumericCellValue());
        }

        // 判断是否合并单元格
        if(StringUtils.equals(startValue,"2024-02-01 10:13:22")){
            // 获取需要做合并操作的单元格在第几行
            int num = cell.getRowIndex();
            // 设置合并的位置
            CellRangeAddress cellAddresses = new CellRangeAddress(num,num,1,2);
            // 合并单元格执行
            sheet.addMergedRegion(cellAddresses);
        }
    }
}
